package com.hdu.point_offer;

public class lcr152 {
    class Solution {
        public boolean verifyTreeOrder(int[] postorder) {
            return f(postorder, 0, postorder.length - 1);
        }

        boolean f(int[] postorder, int l, int r)
        {
            if (l >= r)
            {
                return true;
            }
            int index = -1;
            for (int i = l; i < r; i++)
            {
                if (postorder[i] > postorder[r])
                {
                    index = i;
                    break;
                }
            }
            if (index == -1)
            {
                return f(postorder, l, r - 1);
            }
            for (int i = index + 1; i < r; i++)
            {
                if (postorder[i] <= postorder[r])
                {
                    return false;
                }
            }
            return f(postorder, l, index - 1) && f(postorder, index , r - 1);
        }
    }
}
